package com.qyer.util.log.service.impl;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.qyer.util.log.common.Constants;
import com.qyer.util.log.entity.NginxLog;
import com.qyer.util.log.entity.TrackLog;
import com.qyer.util.log.util.CateUtil;
import com.qyer.util.log.util.HiveFormatUtil;
import com.qyer.util.log.util.MyDateUtil;
import com.qyer.util.log.util.PcLogUtil;
import com.qyer.util.log.util.UgcTypeUtil;


/** 
* 此类描述的是： TrackServiceImpl接口实现类
* @author: hongxi.sun@qyer.com 
* @version: 2015年9月08日 上午10:00:24 
*/
public class TrackServiceImpl {
	/** 
	* 此方法描述的是： 清洗 track nginxlog
	* @author: hongxi.sun@qyer.com 
	* @version: 2015年9月08日 上午10:00:24 
	* @param  String line 
	* @return  TrackLog  格式有问题时返回 null
	*/
	public TrackLog getTrack4NginxFormat(String line) {
		NginxLog nginxLog = PcLogUtil.getNginxLog(line);
		if (nginxLog == null) {
			return null;
		}
		TrackLog trackLog = new TrackLog();
		if(!PcLogUtil.initBaseLog(nginxLog,trackLog)){
			return null ;
		}
		
		if(trackLog.getGuid()==null ||trackLog.getGuid().length()!=36){
			return null ; 
		}
		
		Map<String, String> urlParamMaps = trackLog.getUrlParamMap();
		if (urlParamMaps == null) {
			return null;
	
		}
		
		trackLog.setCategory(urlParamMaps.get("category")) ; 
		trackLog.setLat(urlParamMaps.get("lat")) ; 
		trackLog.setLon(urlParamMaps.get("lng")) ;
		trackLog.setRa_arg(urlParamMaps.get("ra_arg"));
		trackLog.setSession_timestamp(urlParamMaps.get("session"));
		trackLog.setPath(trackLog.getCurUrl());
		
		Map<String, String> extParamMaps = trackLog.getUrlParamMap();
		extParamMaps.remove("category");
		extParamMaps.remove("lat");
		extParamMaps.remove("lng");
		extParamMaps.remove("ra_arg");
		extParamMaps.remove("session");
		
		extParamMaps.remove("_id");
		extParamMaps.remove("type");
		extParamMaps.remove("user_id");
		extParamMaps.remove("guid");
		extParamMaps.remove("referer");
		extParamMaps.remove("referer_link");
		extParamMaps.remove("url");
		extParamMaps.remove("gpstimestamp");
		//extParamMaps.remove("timestamp");
		trackLog.setExtParamMap(extParamMaps);
		
		
		String newlogtype ="0";
		if(trackLog.getType().equalsIgnoreCase("visit")){
			newlogtype="1";
		}else if (trackLog.getType().equalsIgnoreCase("click")){
			newlogtype="2";
		}else if (trackLog.getType().equalsIgnoreCase("link")){
			newlogtype="3";
		}else if (trackLog.getType().equalsIgnoreCase("form")){
			newlogtype="4";
		}else{
			newlogtype ="0";
		}
		trackLog.setType(newlogtype);
		
		return trackLog;
	}

	/** 
	* 此方法描述的是： 清洗 tracklog 初始化  UGC 相关的数据  页面类型 频道  以及ugc_contents
	* @author: hongxi.sun@qyer.com 
	* @version: 2015年9月08日 上午10:00:24 
	* @param  TrackLog trackLog 
	* @return  String  格式有问题时返回 null
	*/
	public TrackLog setCurlUGC(TrackLog trackLog  ,List <String> listUGC , Map <String , String> cityMaps , Map <String, String > countryMaps ,Map<String ,String> cateMap,  Map<String ,String> jn_info_Map, Map<String, String>  sw_map) {
		
		String curl =trackLog.getCurUrl();
		
		if(curl==null||"".equals(curl)){
			return trackLog ;
		}
		String pagetype ; 
		String ugc_content;
		
		String args=UgcTypeUtil.getUgcType(curl, listUGC, cityMaps, countryMaps,jn_info_Map,sw_map);
		
		if(args.indexOf(",")>-1){
			pagetype=args.split(",")[0];
			ugc_content=args.split(",")[1];
		}else{
			pagetype=args;
			ugc_content="-";
		}
		
		
		String refer =trackLog.getRefUrl();
		
		String referArgs=UgcTypeUtil.getUgcType(refer, listUGC, cityMaps, countryMaps,jn_info_Map,sw_map);
		
		String refpagetype ; 
		String ref_ugc_content;
		
		if(referArgs.indexOf(",")>-1){
			refpagetype=referArgs.split(",")[0];
			ref_ugc_content=referArgs.split(",")[1];
		}else{
			refpagetype=referArgs;
			ref_ugc_content="-";
		}
		
		String channel1 =CateUtil.getCate(curl, cateMap);
		trackLog.setChannel1(channel1);
//		trackLog.setChannel2(channel2);
		trackLog.setPageType(pagetype);
		trackLog.setUgc_type(pagetype);
		trackLog.setUgc_content(ugc_content);
		trackLog.setRefer_ugc_content(ref_ugc_content);
		trackLog.setRefer_ugc_type(refpagetype);
		return trackLog;
	}
	
	/**
	 * 正则匹配获取匹配的内容
	 * 
	 * @param regex
	 * @param url
	 * @return
	 */
	public boolean getRegExBool(String regex, String url) {
		boolean result = false;
		Pattern pat = Pattern.compile(regex);
		Matcher mat = pat.matcher(url);
		while (mat.find()) {
			result = true;
		}
		return result;
	}
	
	/**
	 * 正则匹配获取匹配的内容
	 * 
	 * @param regex
	 * @param url
	 * @return
	 */
	public String getRegEx(String regex, String url) {
		String result = "-,-";
		Pattern pat = Pattern.compile(regex);
		Matcher mat = pat.matcher(url);
		while (mat.find()) {
			if (mat.groupCount() > 0) {
				if (null != mat.group(1)) {
					result = mat.group(1);
				}
			}

		}
		return result;
	}

	/** 
	* 此方法描述的是： 针对清洗日志获取对象
	* @author: hongxi.sun@qyer.com 
	* @version: 2015年9月08日 上午10:00:24 
	* @param  String line 
	* @return  TrackLog  格式有问题时返回 null
	*/
	public TrackLog getTrackFormat(String strLine) {
		
		if (strLine == null || strLine.equals("")) {
			return null;
		}
		
		String [] ls = StringUtils.split(strLine , Constants.FS);
		
		if(ls .length<20){
			return null; 
		}

		TrackLog tracklog = new TrackLog();
		tracklog.setUserId(ls[0]);
		tracklog.setGuid(ls[1]);
		tracklog.setUnique_id(ls[1]);
		tracklog.setTrackTime(ls[2]);
		tracklog.setSession_timestamp(ls[3]);
		tracklog.setTrackTime(ls[4]);
		tracklog.setTz(ls[5]);
		tracklog.setUserIp(ls[6]);
		tracklog.setLon(ls[7]);
		tracklog.setLat(ls[8]);
		tracklog.setCurUrl(ls[9]);
		tracklog.setRefUrl(ls[10]);
		tracklog.setType(ls[11]);
		tracklog.setCategory(ls[12]);
		tracklog.setRa_arg(ls[13]);
		tracklog.setUserAgent(ls[14]);
		tracklog.setChannel1(ls[15]);
		tracklog.setUgc_type(ls[16]);
		tracklog.setPlatform(ls[17]);
		tracklog.setUgc_content(ls[18]);
		tracklog.setExtParamMap(HiveFormatUtil.toMapOfFS(ls[19]));
		
		tracklog.setTimeStamp(tracklog.getTrackTime());
		
		return tracklog ;
	}
	
	
}
